//
//  ViewController.swift
//  SwiftCode
//
//  Created by 于虎拴 on 2022/6/8.
//

import UIKit

class ViewController: ListViewController {
  override func getItemCount() -> Int {
    data.count
  }
  
  override func getItem(_ indexPath: IndexPath) -> Item {
    data[indexPath.row]
  }
  
  private lazy var data: [Item] = [
    .init(title: "Swift", description: "编程语言", type: LanguageExample.self),
    .init(title: "UIKit", description: "用户界面", type: UIKitExample.self),
    .init(title: "UILibrary", description: "UI 库", type: UIExample.self),
    .init(title: "Foundation", description: "基础功能。", type: FoundationExample.self),
    .init(title: "AVFoundation", description: "系统播放器", type: AVFoundationExample.self),
    .init(title: "ReplayKit", description: "屏幕录像", type: ReplayKitExample.self),
    .init(title: "Dispatch", description: "调度队列，也称为 Grand Central Dispatch (GCD)", type: DispatchExample.self),
    .init(title: "ToolLibrary ", description: "工具库", type: ToolExample.self),
    .init(title: "Utility", description: "实用程序", type: UtilityExample.self),
  ]
  
  override func viewDidLoad() {
    super.viewDidLoad()
    
//    navigationController?.pushViewController(RxSwiftExample(), animated: false)
//    UtilityExample.presentQRCodeScan()
  }
}

/// https://docs.swift.org/swift-book/documentation/the-swift-programming-language
/// https://swiftgg.gitbook.io/swift/swift-jiao-cheng
class LanguageExample: ListViewController {
  override func getItemCount() -> Int {
    data.count
  }
  
  override func getItem(_ indexPath: IndexPath) -> Item {
    data[indexPath.row]
  }
  
  private lazy var data: [Item] = [
    .init(title: "3. Strings and Characters", description: "字符串和字符", type: StringExample.self),
    .init(title: "4. Collection Types", description: "集合类型", type: CollectionExample.self),
    .init(title: "5. Control Flow", description: "控制流", type: ControlFlowExample.self),
    .init(title: "6. Functions", description: "函数", type: FunctionsExample.self),
    .init(title: "7. Closures", description: "闭包", type: ClosuresExample.self),
    .init(title: "8. Enumerations", description: "枚举", type: EnumerationsExample.self),
    .init(title: "10. Properties", description: "属性", type: PropertiesExample.self),
    .init(title: "15. Initialization", description: "构造&析构过程", type: InitializationExample.self),
    .init(title: "20. TypeCasting", description: "类型转换", type: TypeCastingExample.self),
    .init(title: "23. Generics", description: "泛型", type: GenericsExample.self),
    .init(title: "24. Opaque and Boxed Types", description: "不透明和装箱类型", type: OpaqueTypesExample.self),
    .init(title: "26. Automatic Reference Counting", description: "ARC 自动引用计数", type: ARCExample.self),
    .init(title: "27. Memory Safety", description: "内存安全", type: MemorySafetyExample.self),
  ]
}

class UIKitExample: ListViewController {
  override func getItemCount() -> Int {
    data.count
  }
  
  override func getItem(_ indexPath: IndexPath) -> Item {
    data[indexPath.row]
  }
  
  private lazy var data: [Item] = [
    .init(title: "UIModalPresentationStyle", description: "UIModalPresentationStyle 对生命周期函数的调用", type: UIModalPresentationStyleExample.self),
    .init(title: "UILabel", description: "UILabel 样例", type: UILabelExample.self),
    .init(title: "UIButton", description: "UIButton 属性设置", type: UIButtonExample.self),
    .init(title: "UIView Shadow", description: "UIView 阴影设置", type: ShadowExample.self),
    .init(title: "UIView Animation", description: "UIView 动画", type: AnimationExample.self),
    .init(title: "UITabBarController", description: "UITabBarController 示例", type: UITabBarControllerExample.self),
    .init(title: "UITextField", description: "UITextField 最大输入长度限制", type: UITextFieldExample.self),
    .init(title: "UICollectionView", description: "UICollectionViewExample", type: UICollectionViewExample.self),
  ]
}

class UIExample: ListViewController {
  override func getItemCount() -> Int {
    data.count
  }
  
  override func getItem(_ indexPath: IndexPath) -> Item {
    data[indexPath.row]
  }
  
  private lazy var data: [Item] = [
    .init(title: "TextFieldEffects", description: "文本输入效果", type: TextFieldEffectsExample.self),
    .init(title: "MJRefresh", description: "下拉刷新，上拉加载更多", type: MJRefreshExample.self),
    // TODO: - 以下未完成
    .init(title: "UINestedScrollView1", description: "单纯嵌套", type: UINestedScrollView1VC.self),
    .init(title: "UINestedScrollView2", description: "内外都响应，设置contentOffset，Table无法下拉", type: UINestedScrollView2VC.self),
    .init(title: "UINestedScrollView3", description: "NestScrollView 内嵌 UITableView（可下拉刷新）", type: UINestedScrollView3VC.self),
  ]
}

class ToolExample: ListViewController {
  override func getItemCount() -> Int {
    data.count
  }
  
  override func getItem(_ indexPath: IndexPath) -> Item {
    data[indexPath.row]
  }
  
  private lazy var data: [Item] = [
    .init(title: "UIDeviceOrientation", description: "设备方向控制", type: UIDeviceOrientationExample.self),
    .init(title: "Logger", description: "日志工具", type: LoggerExample.self),
    .init(title: "Regex", description: "正则表达式", type: RegexExample.self),
    .init(title: "RxSwift", description: "响应式编程", type: RxSwiftExample.self),
    .init(title: "RxGesture", description: "手势处理", type: RxGestureExample.self),
    .init(title: "FLEX", description: "应用内调试和探索工具", action: toggleExplorer),
    .init(title: "SDWebImage", description: "具有缓存支持的异步图像下载器", type: SDWebImageExample.self),
    .init(title: "Alamofire", description: "Swift 编写的 HTTP 网络库", type: AlamofireExample.self),
    .init(title: "Moya", description: "Moya 提供一些网络抽象层", type: MoyaExample.self),
  ]
}

class UtilityExample: ListViewController {
  override func getItemCount() -> Int {
    data.count
  }
  
  override func getItem(_ indexPath: IndexPath) -> Item {
    data[indexPath.row]
  }
  
  private lazy var data: [Item] = [
    .init(title: "Image Previewer", description: "图片预览", action: { [weak self] in self?.showImage() }),
    .init(title: "QRCodeScan", description: "二维码扫描", action: Self.presentQRCodeScan),
    .init(title: "FileExplorer", description: "文件管理器", type: FileExplorer.ViewController.self),
    .init(title: "ABTest", description: "支持本地开关", type: ABTestExample.self),
    .init(title: "Share Video", description: "自定义分享，支持文本、图片、视频", type: SharedExample.self),
    .init(title: "Web 浏览器", description: "控制状态栏，导航栏，背景", type: WebExample.self),
  ]
  
  private func showImage() {
    let url = URL(string: "https://pics0.baidu.com/feed/9358d109b3de9c8221aed65fb54044011bd84360.png")!
    let viewController = ImageViewController(url: url)
    viewController.modalPresentationStyle = .overFullScreen
    self.present(viewController, animated: true)
  }
  
  static func presentQRCodeScan() {
    let controller = PortraitNavigation(rootViewController: QRCode.ScanViewController())
    controller.modalPresentationStyle = .overFullScreen
    UIViewController.topMost?.present(controller, animated: true)
  }
}

final class PortraitNavigation: UINavigationController {
  override var shouldAutorotate: Bool { false }
  
  override var supportedInterfaceOrientations: UIInterfaceOrientationMask { .portrait }
}
